<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('校验名称')" prop="name">
            <el-input v-model="form.name" :placeholder="$t('校验名称')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('左边公式')" prop="leftFormulaLabel">
            <el-autocomplete
              v-model="form.leftFormulaLabel"
              class="inline-input"
              :placeholder="$t('左边公式')"
              readonly
              :trigger-on-focus="false"
              @select="selectLeftGS"
            >
              <template slot="append" @click="selectLeftGS">
                <a @click="selectLeftGS">{{ $t('选择公式') }}</a>
              </template>
            </el-autocomplete>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('比较运算符')" prop="compare">
            <dict-select
              v-model="form.compare"
              dictionary="comparison_operators"
              :placeholder="$t('比较运算符')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('右边公式')" prop="rightFormulaLabel">
            <el-autocomplete
              v-model="form.rightFormulaLabel"
              class="inline-input"
              :placeholder="$t('右边公式')"
              readonly
              :trigger-on-focus="false"
              @select="selectRightGS"
            >
              <template slot="append" @click="selectRightGS">
                <a @click="selectRightGS">{{ $t('选择公式') }}</a>
              </template>
            </el-autocomplete>
          </el-form-item>
        </el-col>
        <!--
        <el-col :span="12">
          <el-form-item :label="$t('重点关注')" prop="focusOn">
            <el-radio-group v-model="form.focusOn" :placeholder="$t('重点关注')">
              <el-radio label="Y">{{ $t('是') }}</el-radio>
              <el-radio label="N">{{ $t('否') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('数据类型')" prop="keyType">
            <el-radio-group v-model="form.keyType" :placeholder="$t('数据类型')">
              <el-radio label="money">{{ $t('金额') }}</el-radio>
              <el-radio label="ratio">{{ $t('百分比') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        -->
        <el-col :span="12">
          <el-form-item :label="$t('排序')" prop="sort">
            <el-input-number v-model="form.sort" :placeholder="$t('排序')" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="company:pcw" />
    </div>

    <!-- 添加或修改关键指标公式（系统模板）对话框 -->
    <el-dialog v-el-drag-dialog :title="$t('选择公式')" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogFormGS" append-to-body>
      <pcwTempKeyFormula ref="pcwTempKeyFormula" :company-id="companyId" :formula="formula" :show-date="false" />
    </el-dialog>
  </div>
</template>

<script>
import { savePcwTempSubjectValidation, getPcwTempSubjectValidation } from '@/api/company/pcw/pcwTempSubjectValidation'
import pcwTempKeyFormula from './pcwTempKeyFormula.vue'
export default {
  components: {
    pcwTempKeyFormula
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        name: [
          { required: true, message: this.$t('请输入校验名称'), trigger: 'blur' }
        ],
        leftFormulaLabel: [
          { required: true, message: this.$t('请输入左边公式显示值'), trigger: 'blur' }
        ],
        rightFormulaLabel: [
          { required: true, message: this.$t('请输入右边公式显示值'), trigger: 'blur' }
        ],
        compare: [
          { required: true, message: this.$t('请输入比较运算符'), trigger: 'blur' }
        ]
      },
      type: '',
      companyId: this.$route.query.companyId || this.$route.query.id,
      formula: {},
      formOpen: false
    }
  },
  created() {
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getPcwTempSubjectValidation(data.id).then(response => {
          this.form = response.data
          // eslint-disable-next-line no-eval
          this.form.formulaLeftList = eval(this.form.formulaLeftList)
          // eslint-disable-next-line no-eval
          this.form.formulaRightList = eval(this.form.formulaRightList)
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        companyId: this.companyId,
        leftFormula: undefined,
        leftFormulaLabel: undefined,
        rightFormula: undefined,
        rightFormulaLabel: undefined,
        compare: undefined,
        keyType: 'money',
        focusOn: 'N'
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          this.form.formulaLeftList = JSON.stringify(this.form.formulaLeftList)
          this.form.formulaRightList = JSON.stringify(this.form.formulaRightList)
          savePcwTempSubjectValidation(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    },
    /** 选择公式操作 */
    selectLeftGS() {
      this.type = 'left'
      this.formOpen = true
      this.formula = {
        name: this.form.leftFormulaLabel,
        code: this.form.leftFormula,
        formulaList: this.form.formulaLeftList || []
      }
    },
    selectRightGS() {
      this.type = 'right'
      this.formOpen = true
      this.formula = {
        name: this.form.rightFormulaLabel,
        code: this.form.rightFormula,
        formulaList: this.form.formulaRightList || []
      }
    },
    addFormula(name, code, formulaList) {
      console.log(formulaList)
      if (this.type === 'left') {
        this.form.leftFormulaLabel = name
        this.form.leftFormula = code
        this.form.formulaLeftList = formulaList
      } else {
        this.form.rightFormulaLabel = name
        this.form.rightFormula = code
        this.form.formulaRightList = formulaList
      }
    }

  }

}
</script>
